//
//  ViewController.swift
//  PhotoVideo
//
//  Created by zenghui on 09/19/2020.
//  Copyright (c) 2020 zenghui. All rights reserved.
//

import UIKit
import AVFoundation
import PhotoVideo

class ViewController: UIViewController, PhotoVideoViewControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        setUI()
    }
    
    //MARK: -懒加载UI
    lazy var photoButton: UIButton = {
        let photoButton = UIButton(type: .custom)
        photoButton.frame = CGRect(origin: CGPoint(x: (UIScreen.main.bounds.width-100)/2, y: 100), size: CGSize(width: 100, height: 40))
        photoButton.setTitle("添加照片", for: .normal)
        photoButton.backgroundColor = .blue
        photoButton.addTarget(self, action: #selector(photoButtonAction), for: .touchUpInside)
        return photoButton
    }()
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 145), size: CGSize(width: 300, height: 300)))
        imageView.backgroundColor = .black
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    //MARK: -设置UI
    func setUI() {
        view.backgroundColor = .white
        
        view.addSubview(photoButton)
        view.addSubview(imageView)
    }
    
    //MARK: -点击事件
    @objc func photoButtonAction() {
        /*
         public let AVAssetExportPresetLowQuality: String
         public let AVAssetExportPresetMediumQuality: String
         public let AVAssetExportPresetHighestQuality: String
         public let AVAssetExportPresetHEVCHighestQuality: String
         public let AVAssetExportPresetHEVCHighestQualityWithAlpha: String
         public let AVAssetExportPreset640x480: String
         public let AVAssetExportPreset960x540: String
         public let AVAssetExportPreset1280x720: String
         public let AVAssetExportPreset1920x1080: String
         public let AVAssetExportPreset3840x2160: String
         public let AVAssetExportPresetHEVC1920x1080: String
         public let AVAssetExportPresetHEVC1920x1080WithAlpha: String
         public let AVAssetExportPresetHEVC3840x2160: String
         public let AVAssetExportPresetHEVC3840x2160WithAlpha: String
         public let AVAssetExportPresetAppleM4A: String
         public let AVAssetExportPresetPassthrough: String
         */
        let vc = PhotoVideoViewController(type: .picture, colorType: .red, presetType: AVAssetExportPreset640x480)
        vc.delegate = self
        vc.maxSelectPhotoCount = 8
        vc.maxSelectVideoCount = 2
        vc.modalPresentationStyle = .fullScreen
        self.present(vc, animated: true, completion: nil)
        
    }
    
    func didFinishPhoto(mediaType: Int, images: [UIImage], urls: [URL]) {
        if mediaType == 1 {
            imageView.image = images[0]
            let image = images[0]
            let imgData = NSData(data: (image).jpegData(compressionQuality: 1)!)
            print(imgData.length)
        } else if mediaType == 2 {
            DispatchQueue.main.async {
                let playerItem = AVPlayerItem(url: urls[0])
                let player = AVPlayer(playerItem: playerItem)
                player.rate = 1.0
                let playerLayer = AVPlayerLayer(player: player)
                playerLayer.videoGravity = .resizeAspect
                playerLayer.frame = self.imageView.bounds
                self.imageView.layer.addSublayer(playerLayer)
                player.play()
            }
        }
    }
}

